<?php

namespace Plugins\UEditorPlus\Admin\Controller;

use App\Common\Provider\Uploader\UploaderInterface;
use App\Common\Provider\Uploader\UploadResult;
use App\Common\Util\Response;
use App\Common\Util\TP;

/**
 * Class UEditorController
 */
class UEditorController
{
    /**
     * 处理编辑器请求
     *
     * @return \think\Response
     */
    public function index(): \think\Response
    {
        $action = TP::request()->get('action');
        $data   = TP::request()->post();
        $config = $this->getConfig();

        return match ($action) {
            'config'                          => $this->config($data),
            $config['imageActionName']        => $this->uploadImage($data),
            $config['scrawlActionName']       => $this->uploadScrawl($data),
            $config['snapscreenActionName']   => $this->uploadSnap($data),
            $config['videoActionName']        => $this->uploadVideo($data),
            $config['fileActionName']         => $this->uploadFile($data),
            $config['imageManagerActionName'] => $this->listImage($data),
            $config['fileManagerActionName']  => $this->listImage($data),
            $config['catcherActionName']      => $this->catchImage($data),
        };
    }

    /**
     * 获取配置
     *
     * @param $data
     *
     * @return \think\Response
     */
    public function config($data): \think\Response
    {
        $config = $this->getConfig();

        return Response::json($config);
    }

    public function uploadImage($data)
    {
        $uploadResult = $this->upload();
        return $this->resourceReturn($uploadResult->fileFullPath, $uploadResult->originFilename);
    }

    public function uploadScrawl($data)
    {
        $uploadResult = $this->upload();
        return $this->resourceReturn($uploadResult->fileFullPath, $uploadResult->originFilename);
    }

    public function uploadVideo($data)
    {
        $uploadResult = $this->upload();
        return $this->resourceReturn($uploadResult->fileFullPath, $uploadResult->originFilename);
    }

    public function uploadFile($data)
    {
        $uploadResult = $this->upload();
        return $this->resourceReturn($uploadResult->fileFullPath, $uploadResult->originFilename);
    }

    public function listImage($data)
    {
        return Response::json([
            'state' => 'SUCCESS',
            'list'  => []
        ]);
    }

    public function catchImage($data)
    {
        $uploadResult = $this->upload();
        return $this->resourceReturn($uploadResult->fileFullPath, $uploadResult->originFilename);
    }

    private function resourceReturn(string $path, string $title): \think\Response
    {
        return Response::json([
            "state"    => "SUCCESS",
            "url"      => $path,
            "title"    => $title,
            "original" => $title
        ]);
    }

    private function fail(string $message): \think\Response
    {
        return Response::json([
            'state'   => 'FAIL',
            'message' => $message
        ]);
    }

    /**
     * @return mixed
     */
    private function getConfig(): mixed
    {
        $config = TP::config('plugins.UEditorPlus');
        if (!$config) {
            $config = include __DIR__ . '/../../config.php';
        }
        return $config;
    }

    private function uploadSnap(mixed $data)
    {
        $uploadResult = $this->upload();
        return $this->resourceReturn($uploadResult->fileFullPath, $uploadResult->originFilename);
    }

    private function upload(string $name = 'file'): UploadResult
    {
        /** @var UploaderInterface $uploader */
        $uploader = app(UploaderInterface::class);
        return $uploader->upload(TP::request()->file($name));
    }
}